package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.valid.AddGroup;
import com.sky.valid.UpdateGroup;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *
 * TODO 找时间完善swagger
 *
 * 员工管理
 */
@Api(tags = "与员工接口相关")
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("登录功能")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("登出功能")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 修改密码
     * @param dto
     * @return
     */
    @ApiOperation("修改密码功能")
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody @Validated PasswordEditDTO dto){
        employeeService.editPassword(dto);
        return Result.success();
    }

    /**
     * 添加一个员工信息
     * @param employeeDTO
     * @return
     */
    @ApiOperation("添加员工信息")
    @PostMapping
    public Result save(@RequestBody @Validated(AddGroup.class) EmployeeDTO employeeDTO){
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @ApiOperation("分页功能")
    @GetMapping("/page")
    public Result page(@Validated EmployeePageQueryDTO dto){
        PageResult result = employeeService.page(dto);
        return Result.success(result);
    }

    /**
     * 修改员工状态
     * @param sStatus
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "被修改的员工id", required = true),
            @ApiImplicitParam(name = "status", value = "需要改成的状态", required = true)
    })
    public Result status( String id, @PathVariable("status") String sStatus){
        int status = Integer.parseInt(sStatus);
        long pid = Long.parseLong(id);
        employeeService.status(status, pid);
        return Result.success();
    }

    /**
     * 回显修改的员工数据
     * @param id
     * @return
     */
    @ApiOperation("修改回显数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "员工id", required = true)
    })
    @GetMapping("/{id}")
    public Result info(@PathVariable("id")  String id){
        log.info("回显修改员工数据，登录id-->{}...", BaseContext.getCurrentId());
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("睡眠后，登录id-->{}...", BaseContext.getCurrentId());
        Employee employee = employeeService.getOne(Long.parseLong(id));
        return Result.success(employee);
    }

    /**
     * 根据id修改员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工")
    public Result update(@RequestBody @Validated(UpdateGroup.class) EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
